﻿using Nop.Core.Domain;
using Nop.Core.Domain.Customers;
using Nop.Core.Infrastructure;
using Nop.Services.Configuration;
using Nop.Services.Customers;
using System;
using System.Web.Mvc;

namespace Nop.Web.Framework.Security
{
    public class UserValidator : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            try
            {
                Customer customer = null;
                // 检验小程序是否开启
                var settingService = EngineContext.Current.Resolve<ISettingService>();
                var storeInformationSettings = settingService.LoadSetting<StoreInformationSettings>();
                if (!storeInformationSettings.StoreClosed)
                {
                    var token = filterContext.HttpContext.Request.Headers["token"];
                    if (!string.IsNullOrEmpty(token))
                    {
                        var customerService = EngineContext.Current.Resolve<ICustomerService>();
                        customer = customerService.GetCustomerByToken(token);
                    }
                }
                else
                    filterContext.ActionParameters["warning"] = "小程序已关闭";
                filterContext.ActionParameters["customer"] = customer;
            }
            catch (Exception)
            {
            }
            base.OnActionExecuting(filterContext);
        }
    }
}
